#!/bin/bash

set -x

# default to update source then build

TOP_DIR=$(pwd)
SRC_DIR=$TOP_DIR/lib_source
BD_DIR=$TOP_DIR/lib_build
BIN_DIR=$TOP_DIR/lib_bin

export PATH=$TOP_DIR/cross/bin:$PATH

VER_OPT=1.0.1e

cmd=all64
if [ -n "$1" ] ; then
	cmd=$1
fi

if [ "$cmd" == "all64" ] || [ "$cmd" == "all32" ] || [ "$cmd" == "update" ] ; then
	mkdir -p $SRC_DIR || exit 0
	cd $SRC_DIR
	if [ ! -f openssl-$VER_OPT.tar.gz ] ; then
		wget http://www.openssl.org/source/openssl-$VER_OPT.tar.gz
		# tar xzf openssl-1.0.0d.tar.gz
	fi
	cd ..
fi

if [ "$cmd" == "all64" ] || [ "$cmd" == "b64" ] || [ "$cmd" == "all32" ] || [ "$cmd" == "b32" ] ; then
	HOST_PARAM="--cross-compile-prefix=x86_64-w64-mingw32- mingw64"
	if [ "$cmd" == "all32" ] || [ "$cmd" == "b32" ] ; then
		HOST_PARAM="--cross-compile-prefix=i686-w64-mingw32- mingw"
	fi
	rm -rf $BIN_DIR/openssl
	mkdir -p $BD_DIR
	cd $BD_DIR
	rm -rf bd_openssl
	tar xzf $SRC_DIR/openssl-$VER_OPT.tar.gz
	mv openssl-$VER_OPT bd_openssl
	cd bd_openssl
	./Configure --prefix=$BIN_DIR/openssl shared $HOST_PARAM
		
#	--enable-static --disable-shared
#	--enable-shared --disable-static

	make $MAKE_OPT
	make install
	cd ..
	cd ..
fi

